<template>
  <el-dialog
    v-el-drag-dialog
    :title="'移动配送客户'"
    :visible.sync="visible"
    width="800px"
    height="500"
    append-to-body
    @closed="reset"
  >
    <div class="main">
      <span v-if="text">从{{ text }}(当前计划)</span>
      <span>移动到</span>
      <el-select v-model="planCode" size="mini" clearable>
        <el-option v-for="l in EnumList" :key="l.planCode" :value="l.planCode" :label="l.planName" />
      </el-select>
      <span>配送计划</span>

    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.add || $loadingPlugin.edit" type="primary" @click="success">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { orderSendPlanList, orderSendPlanMove } from '@/api'
  export default {
    name: 'move',

    data() {
      return {
        visible: false,
        EnumList: [],
        text: null,
        key: 0,
        planCode: null,
        submitAuditList: []
      }
    },
    async created() {

    },

    methods: {
      async open(temp) {
        return new Promise(async resolve => {
          if (this.key === 0) {
            await this.getEnum()
            this.key++
          }
          this.resolve = resolve
          this.visible = true
          this.submitAuditList = [...temp]
          if (this.submitAuditList.length === 1) {
            this.text = this.submitAuditList[0].planName
          }
        })
      },
      async success() {
        const list = this.submitAuditList.map(l => l.csmAddressId)
        const params = {
          planCode: this.planCode,
          csmAddressIds: list.join(',')
        }
        const res = await orderSendPlanMove(params)
        if (res.success) {
          this.$message.success('移动成功')
          this.$emit('refresh')
          this.visible = false
        }
      },
      async getEnum() {
        const params = {
          currentPage: 1,
          pageSize: 999
        }
        const res = await orderSendPlanList(params)
        this.EnumList = res.data.rows
      },
      reset() {
        this.text = null
        this.submitAuditList = []
        this.visible = false
      }
    }
  }
</script>

<style lang="scss" scoped>
  .main {
    display: flex;
    align-items: center;
    justify-content: center;
  }
</style>
